import DebugExt from "../../Script/NRLib/base/DebugExt";
import PaoMaMsg from "./PaoMaMsg";
import SrollTips from "./SrollTips";

const { ccclass, property, menu } = cc._decorator;

@ccclass
@menu('GlobalUI/GlobalMoveTextCtl')
export default class GlobalMoveTextCtl extends cc.Component {

    // 跑马灯prefab
    @property(cc.Prefab)
    public paomaPrefab: cc.Prefab = null;

    // LIFE-CYCLE CALLBACKS:

    protected onLoad(): void {
        // 监听服务器跑马灯消息
        yy.event.on(null, yy.event.sys.RecvSysNotice, (e: yy.event.sys.RecvSysNotice) => {
            DebugExt.log("[SrollTips] 跑马灯信息：", e ? JSON.stringify(e) : null);
            if (!this.showSystemNotice)
                return;
            if (e && e.isShowByMoveText())
                PaoMaMsg.pushMsgInfoAndC(e.content, e.fromGameID);
        });
    }

    protected start() {
        this.initSrollTips();
    }

    private get showSystemNotice(): boolean {
        return yy.getSiteXML().getDataNodeContent("IsShowSystemNotice", "1") != "0";
    }

    /**
     * 跑马灯对象列表初始化（两个不同背景色的壳）
     */
    private initSrollTips() {
        if (!this.showSystemNotice)
            return;

        // 小游戏的跑马灯效果(0蓝背景、1红背景)
        for (let i = 0; i < 2; i++) {
            const com: SrollTips = new SrollTips(this.paomaPrefab, i);
            yy.ui.manager.regMoveTextUI((parent?: cc.Node) => {
                if (parent) {
                    let node = com.getNode();
                    if (node) {
                        node.parent = parent;
                    }
                }
                return com;
            }, i);
        }
    }
}
